package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ProductVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/24 16:58
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageInfo<Product> findProductList(String pageNum, String pageSize, String name, String categoryId, String supplier, String status,String categorys) {
        if(!StringUtils.isEmpty(pageNum)&&!StringUtils.isEmpty(pageSize)){
            PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        }
        //开始查询
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if(!StringUtils.isEmpty(categoryId)){
            //criteria.andEqualTo("categoryId",categoryId);
        }
        if(!StringUtils.isEmpty(categorys)){
            String[] split = categorys.split(",");
            if(split.length==3){
                criteria.andEqualTo("oneCategoryId",split[0]);
                criteria.andEqualTo("twoCategoryId",split[1]);
                criteria.andEqualTo("threeCategoryId",split[2]);
            }
        }
        if(!StringUtils.isEmpty(supplier)){
            criteria.andLike("name","%"+supplier+"%");
        }
        if(!StringUtils.isEmpty(status)){
            criteria.andEqualTo("status",status);
        }
        List<Product> productList = productMapper.selectByExample(example);
        return new PageInfo<Product>(productList);
    }

    @Override
    public void add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        Long[] categoryKeys = productVO.getCategoryKeys();
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
        product.setStatus(2);//待审核
        product.setpNum(UUID.randomUUID().toString());
        productMapper.insert(product);
    }

    @Override
    public ProductVO edit(Long id) {
        //Product product = productMapper.selectByPrimaryKey(id);
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        Product product = productMapper.selectOneByExample(example);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        Long[] categoryKeys = new Long[3];
        categoryKeys[0] = product.getOneCategoryId();
        categoryKeys[1] = product.getTwoCategoryId();
        categoryKeys[2] = product.getThreeCategoryId();
        productVO.setCategoryKeys(categoryKeys);
        return productVO;
    }

    @Override
    public void update(ProductVO productVo) {
        Product product = new Product();
        BeanUtils.copyProperties(productVo,product);
        product.setModifiedTime(new Date());
        Long[] categoryKeys = productVo.getCategoryKeys();
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
        product.setStatus(2);
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",product.getId());
        productMapper.updateByExample(product,example);
    }

    @Override
    public void delete(Long id) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        productMapper.deleteByExample(example);
    }

    @Override
    public void remove(Long id) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        Product product = new Product();
        product.setStatus(1);
        productMapper.updateByExampleSelective(product,example);
    }

    @Override
    public void publish(Long id) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        Product product = new Product();
        product.setStatus(0);
        productMapper.updateByExampleSelective(product,example);
    }

    @Override
    public void back(Long id) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        Product product = new Product();
        product.setStatus(2);
        productMapper.updateByExampleSelective(product,example);
    }
}
